package cn.lvji.springboot.learn3;

import cn.lvji.springboot.learn3.controller.MyServlet;
import cn.lvji.springboot.learn3.controller.TestServletHandlerAdapter;
import cn.lvji.springboot.learn3.listener.MyApplicationEnvironmentPreparedEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.handler.SimpleServletHandlerAdapter;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * 启动类
 *
 * @author zhouzx
 */
@SpringBootApplication
//@EnableWebMvc
public class Learn3Application {
    private static final Logger logger = LoggerFactory.getLogger(Learn3Application.class);

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Learn3Application.class);
        application.addListeners(new MyApplicationEnvironmentPreparedEventListener());
        application.run(args);
        System.out.println("run............");
    }

//    @Bean
//    UndertowServletWebServerFactory undertowServletWebServerFactory() {
//        UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
//        factory.addBuilderCustomizers(
//                builder -> {
//                    builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true)
//                            .setServerOption(UndertowOptions.HTTP2_SETTINGS_ENABLE_PUSH, true);
//                });
//
//        return factory;
//    }


    @Bean
    protected ServletContextListener listener() {
        return new ServletContextListener() {

            @Override
            public void contextInitialized(ServletContextEvent sce) {
                logger.info("ServletContext initialized");
            }

            @Override
            public void contextDestroyed(ServletContextEvent sce) {
                logger.info("ServletContext destroyed");
            }

        };
    }

    @Bean
    public HandlerAdapter simpleServletHandlerAdapter() {
        return new SimpleServletHandlerAdapter();
    }

    @Bean
    public ServletRegistrationBean servlet1() {
        return new ServletRegistrationBean(new MyServlet(), "/my1");
    }

    @Bean
    public ServletRegistrationBean servlet2() {
        return new ServletRegistrationBean(new TestServletHandlerAdapter(), "/servlet2");
    }

    /*@Bean
    public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        SerializeConfig serializeConfig = SerializeConfig.globalInstance;
        serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
        serializeConfig.put(Long.class, ToStringSerializer.instance);
        serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
        fastJsonConfig.setSerializeConfig(serializeConfig);
        converter.setFastJsonConfig(fastJsonConfig);
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue,SerializerFeature.WriteBigDecimalAsPlain);
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        converter.setFastJsonConfig(fastJsonConfig);
        List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        converter.setSupportedMediaTypes(supportedMediaTypes);

        return converter;
    }
*/
}


